import {$post, $setToken} from '@/utils/require'
import {message} from "ant-design-vue"
// export const roleIdRef = ref();
export const $login = async (params:any) => {
    let result = await $post('user/login', params)
    let {code, message: msg, data} = result
    if (code == 200) {
        let token = data
        message.success(msg)
        sessionStorage.setItem("token", token)
        $setToken(token)

        // 分割JWT令牌
        const parts = token.split('.');
        // 解码载荷部分获取用户权限
        const {roleId,userId} = JSON.parse(atob(parts[1]));
        await sessionStorage.setItem("userId",userId)
        await sessionStorage.setItem("roleId",roleId )
        // roleIdRef.value=roleId
        if (params.remember) {
            localStorage.setItem("remember", params.remember)
            localStorage.setItem("account", params.account)
            localStorage.setItem("password", params.password)
        }
    } else {
        message.error(msg)
    }
    return code
}


export const $getLoginUser = async (params:any) => {
    let {data} = await $post('user/list', params)
    return data
}
